package com.beiding.t;

import com.beiding.jsonanalyzer.Analyzer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * @author 丁常磊
 * @date 2021/12/15 15:40
 */

public class Kibana日志处理 {
    public static void main(String[] args) {

        handle(new File("C:\\Users\\lenovo\\Desktop\\日期处理\\新建文件夹\\新建 文本文档.txt"), new File("C:\\Users\\lenovo\\Desktop\\日期处理\\js\\输出.json"));
    }

    private static void handle(File file, File out) {
        try {

            FileWriter fileWriter = new FileWriter(out);

            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

            String line;

            fileWriter.write("{\"logs\":[");

            while ((line = bufferedReader.readLine()) != null) {

                int index = line.indexOf("{");
                if (index >= 0) {

                    int indexOf = line.indexOf("message:");

                    String tt = line.substring(indexOf + "message:".length(), index);

                    System.out.println(tt);

                    fileWriter.write("{\n\"title\":\"");
                    fileWriter.write(tt);
                    fileWriter.write("\",\n\"content\":");
                    String substring = line.substring(index);

                    int i = substring.lastIndexOf("}");

                    if (i > 0) {
                        String t = substring.substring(0, i + 1);
                        try {


                            Analyzer parse = Analyzer.parse(t);

                          /*  List<Key> keys = read.findKeys(Query.eq("mode", "2").or().eq("pop_mode", "1"));

                            for (Key key : keys) {
                                read.set(key, null);
                            }*/

                            System.out.println(parse.format());
                            fileWriter.write(parse.format());
                            fileWriter.write("},\n");
                            fileWriter.write("\n");
                            fileWriter.write("\n");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }

            }
            fileWriter.write("]}");

            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
